package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.util.ArrayList;
import java.util.List;

import ch.rheintalconnection.sysventory.dto.PrinterDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class PrinterScan {
	
	public static final String QUERY = "SELECT * FROM Win32_Printer";
	
	private ActiveXComponent axWMI;
	
	
	public PrinterScan(ActiveXComponent axWMI) {
		this.axWMI = axWMI;
	}
	
	public List<PrinterDto> scan(){
		
		ArrayList<PrinterDto> printerDtos = new ArrayList<PrinterDto>();
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
		
			String description = Dispatch.call(item, "Name").toString();
			String driver = Dispatch.call(item, "DriverName").toString();
			
			printerDtos.add(new PrinterDto(description, driver));
		}
		return printerDtos;	
	}
		
}
